package com.order_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.order_system.constants.Code;
import com.order_system.entity.Customer;
import com.order_system.entity.ResModel;
import com.order_system.mapper.CustomerMapper;
import com.order_system.service.CustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李阳
 * @since 2023-01-06
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Override
    public ResModel login(Customer customer) {
        LambdaQueryWrapper<Customer> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(true,Customer::getCustomerName,customer.getCustomerName());
        wrapper.eq(true,Customer::getPassword,customer.getPassword());
        Customer one = getOne(wrapper);   // 查数据库 select *
        if (one!=null){
            return ResModel.success(Code.LOGIN,one);
        }
        return ResModel.error(Code.NON_EXISTENT);
    }

    @Override
    public ResModel register(Customer customer) {
        LambdaQueryWrapper<Customer> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(true,Customer::getCustomerName,customer.getCustomerName());
        wrapper.eq(true,Customer::getPassword,customer.getPassword());
        Customer one = getOne(wrapper);
        if (one==null){
            boolean save = save(customer);
            return ResModel.success(Code.REGISTRATION,save);
        }
        return ResModel.error(Code.REGISTERED);
    }
}
